<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>正则表达式GolangPythonJavascript |  朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-正则表达式GolangPythonJavascript"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  正则表达式GolangPythonJavascript
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/09/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8FGolangPythonJavascript/" class="article-date">
  <time datetime="2023-05-09T13:57:22.000Z" itemprop="datePublished">2023-05-09</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Golang/">Golang</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">12 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="Golang"><a href="#Golang" class="headerlink" title="Golang"></a>Golang</h3><p><a target="_blank" rel="noopener" href="https://juejin.cn/post/7090196238955446285">Golang</a><br>regexp.MatchString(“.even”, word)</p>
<h3 id="Python"><a href="#Python" class="headerlink" title="Python"></a>Python</h3><p><a target="_blank" rel="noopener" href="https://www.runoob.com/python/python-reg-expressions.html">Python</a><br>re.match(pattern, string, flags&#x3D;0)</p>
<h3 id="JavaScript"><a href="#JavaScript" class="headerlink" title="JavaScript"></a>JavaScript</h3><p><a target="_blank" rel="noopener" href="https://www.runoob.com/js/js-regexp.html">JavaScript</a><br>str.search(&#x2F;Runoob&#x2F;i); 说明,i 是不区分大小写</p>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>在计算中，我们经常需要将特定模式的字符或字符子集匹配为另一个字符串中的字符串。此技术用于使用特别的语法来搜索给定字符串中的特定字符集。比如邮件、手机号、身份证号等等。</p>
<p>如果搜索到的模式匹配，或者在目标字符串中找到给定的子集，则搜索被称为成功；否则被认为是不成功的。</p>
<p>那么此时该用到正则表达式了。</p>
<h2 id="什么是正则表达式"><a href="#什么是正则表达式" class="headerlink" title="什么是正则表达式"></a>什么是正则表达式</h2><p><strong>正则表达式</strong>（或 RegEx）是一个特殊的字符序列，它定义了用于匹配特定文本的搜索模式。在 Golang 中，有一个内置的正则表达式包: <code>regexp</code> 包，其中包含所有操作列表，如过滤、修改、替换、验证或提取。</p>
<p>正则表达式可以用于文本搜索和更高级的文本操作。正则表达式内置于 grep 和 sed 等工具，vi 和 emacs 等文本编辑器，Go、Java 和 Python 等编程语言中。表达式的语法主要遵循这些流行语言中使用的已建立的 RE2 语法。 <a href="https://link.juejin.cn/?target=https://github.com/google/re2/wiki/Syntax">RE2</a> 语法是 PCRE 的一个子集，有各种注意事项。</p>
<p>以下是正则表达式模式表格：</p>
<table>
<thead>
<tr>
<th>Regex</th>
<th>Meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>.</code></td>
<td>匹配任何单一字符</td>
</tr>
<tr>
<td><code>?</code></td>
<td>匹配前面的元素一次要么根本不匹配。</td>
</tr>
<tr>
<td><code>+</code></td>
<td>匹配前面的元素一次或多次。</td>
</tr>
<tr>
<td><code>*</code></td>
<td>匹配前一个元素的零次或多次。</td>
</tr>
<tr>
<td><code>^</code></td>
<td>匹配字符串中的起始位置。</td>
</tr>
<tr>
<td><code>$</code></td>
<td>匹配字符串中的结束位置。</td>
</tr>
<tr>
<td>&#96;</td>
<td>&#96;</td>
</tr>
<tr>
<td><code>[abc]</code></td>
<td>匹配 a 或 b，或 c。</td>
</tr>
<tr>
<td><code>[a-c]</code></td>
<td>a-c 的范围; 匹配 a 或 b，或 c。</td>
</tr>
<tr>
<td><code>[^abc]</code></td>
<td>否定, 匹配除 a，或 b，或 c之外的一切。</td>
</tr>
<tr>
<td><code>\s</code></td>
<td>匹配空格字符。</td>
</tr>
<tr>
<td><code>\w</code></td>
<td>匹配一个单词字符；等同于 <code>[a-zA-Z_0-9]</code></td>
</tr>
</tbody></table>
<p>Go 语言的 <code>regexp</code> 包中有几个典型的函数：</p>
<ul>
<li><code>MatchString()</code></li>
<li><code> Compile()</code></li>
<li><code>FindString()</code></li>
<li><code>FindAllString()</code></li>
<li><code>FindStringIndex()</code></li>
<li><code>FindAllStringIndex()</code></li>
<li><code>FindStringSubmatch()</code></li>
<li><code>Split()</code></li>
<li><code>ReplaceAllString</code></li>
<li><code>ReplaceAllStringFunc()</code></li>
</ul>
<h2 id="MatchString-函数"><a href="#MatchString-函数" class="headerlink" title="MatchString 函数"></a>MatchString 函数</h2><p><code>MatchString()</code> 函数用来匹配子字符串，下面的程序用来匹配是否是 <strong>一个字母后跟着 even 字符</strong>，正则模式为： <code>.even</code>。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;log&quot;</span></span><br><span class="line">	<span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">	words := [...]<span class="type">string</span>&#123;<span class="string">&quot;Seven&quot;</span>, <span class="string">&quot;even&quot;</span>, <span class="string">&quot;Maven&quot;</span>, <span class="string">&quot;Amen&quot;</span>, <span class="string">&quot;eleven&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> _, word := <span class="keyword">range</span> words &#123;</span><br><span class="line"></span><br><span class="line">		found, err := regexp.MatchString(<span class="string">&quot;.even&quot;</span>, word)</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			log.Fatal(err)</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> found &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s matches\n&quot;</span>, word)</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s does not match\n&quot;</span>, word)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>运行该代码：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Seven <span class="keyword">matches</span></span><br><span class="line">even does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">Maven does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">Amen does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">eleven <span class="keyword">matches</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>但同时我们能看到编辑器有提示，说明直接用 <code>MatchString</code> 匹配字符串会影响性能。：</p>
<p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/fd46db2f624845bea349deb9d98b12cf~tplv-k3u1fbpfcp-zoom-in-crop-mark:4536:0:0:0.awebp" alt="image.png"></p>
<p>所以我们考虑用 <code>Compile()</code> 或者 <code>MustCompile()</code>创建一个编译好的正则表达式对象，然后再来进行模式匹配。</p>
<h2 id="Compile-函数"><a href="#Compile-函数" class="headerlink" title="Compile 函数"></a>Compile 函数</h2><p><code>Compile</code> 函数解析正则表达式，如果成功，则返回可用于匹配文本的 Regexp 对象。编译的正则表达式产生更快的代码。假如正则表达式非法，那么 <code>Compile()</code> 方法回返回 error ,而 <code>MustCompile()</code> 编译非法正则表达式时不会返回 error ，而是返回 panic 。</p>
<p>先来看 <code>Compile</code> 函数：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;log&quot;</span></span><br><span class="line">	<span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">	words := [...]<span class="type">string</span>&#123;<span class="string">&quot;Seven&quot;</span>, <span class="string">&quot;even&quot;</span>, <span class="string">&quot;Maven&quot;</span>, <span class="string">&quot;Amen&quot;</span>, <span class="string">&quot;eleven&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">	re, err := regexp.Compile(<span class="string">&quot;.even&quot;</span>)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">		log.Fatal(err)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> _, word := <span class="keyword">range</span> words &#123;</span><br><span class="line"></span><br><span class="line">		found := re.MatchString(word)</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> found &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s matches\n&quot;</span>, word)</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s does not match\n&quot;</span>, word)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们使用了编译的正则表达式。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">re, err := regexp.Compile(<span class="string">&quot;.even&quot;</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>即使用 <code>Compile</code> 编译正则表达式。然后在返回的正则表达式对象上调用 <code>MatchString</code> 函数：</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">found := re.<span class="built_in">MatchString</span>(<span class="type">word</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>运行程序，能看到同样的代码：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Seven <span class="keyword">matches</span></span><br><span class="line">even does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">Maven does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">Amen does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">eleven <span class="keyword">matches</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="MustCompile-函数"><a href="#MustCompile-函数" class="headerlink" title="MustCompile 函数"></a>MustCompile 函数</h2><p>再看使用 <code>MustCompile</code> 函数的例子，它编译正则表达式并在无法解析表达式时发生 panic。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">	words := [...]<span class="type">string</span>&#123;<span class="string">&quot;Seven&quot;</span>, <span class="string">&quot;even&quot;</span>, <span class="string">&quot;Maven&quot;</span>, <span class="string">&quot;Amen&quot;</span>, <span class="string">&quot;eleven&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">	re := regexp.MustCompile(<span class="string">&quot;.even&quot;</span>)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> _, word := <span class="keyword">range</span> words &#123;</span><br><span class="line"></span><br><span class="line">		found := re.MatchString(word)</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> found &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s matches\n&quot;</span>, word)</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">			fmt.Printf(<span class="string">&quot;%s does not match\n&quot;</span>, word)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>运行该代码:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ go run main.go </span><br><span class="line">Seven <span class="keyword">matches</span></span><br><span class="line">even does <span class="keyword">not</span> <span class="keyword">match</span> </span><br><span class="line">Maven does <span class="keyword">not</span> <span class="keyword">match</span></span><br><span class="line">Amen does <span class="keyword">not</span> <span class="keyword">match</span> </span><br><span class="line">eleven <span class="keyword">matches</span> </span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="FindString-查找字符串"><a href="#FindString-查找字符串" class="headerlink" title="FindString 查找字符串"></a>FindString 查找字符串</h2><p><code>FindString()</code> 用来返回第一个匹配的结果。如果没有匹配的字符串，那么它回返回一个空的字符串，当然如果你的正则表达式就是要匹配空字符串的话，它也会返回空字符串。使用 <code>FindStringIndex</code> 或者 <code>FindStringSubmatch</code>可以区分这两种情况。下面是<code>FindString()</code>的例子：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;fmt&quot;</span></span><br><span class="line">	<span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">	str := <span class="string">&quot;Today is Tuesday!&quot;</span></span><br><span class="line"></span><br><span class="line">	regexp, _ := regexp.Compile(<span class="string">&quot;^T([a-z]+)y&quot;</span>)</span><br><span class="line"></span><br><span class="line">	fmt.Println(regexp.FindString(str))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>运行该代码，可以看到如下结果：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="keyword">go</span> run main.<span class="keyword">go</span> </span><br><span class="line">Today</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="FindAllString-函数"><a href="#FindAllString-函数" class="headerlink" title="FindAllString 函数"></a>FindAllString 函数</h2><p><code>FindAllString</code> 函数返回正则表达式的所有连续匹配的切片。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;os&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> content = <span class="string">`Foxes are omnivorous mammals belonging to several genera </span></span><br><span class="line"><span class="string">of the family Canidae. Foxes have a flattened skull, upright triangular ears, </span></span><br><span class="line"><span class="string">a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every </span></span><br><span class="line"><span class="string">continent except Antarctica. By far the most common and widespread species of </span></span><br><span class="line"><span class="string">fox is the red fox.`</span></span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">&quot;(?i)fox(es)?&quot;</span>)</span><br><span class="line"></span><br><span class="line">    found := re.FindAllString(content, <span class="number">-1</span>)</span><br><span class="line"></span><br><span class="line">    fmt.Printf(<span class="string">&quot;%q\n&quot;</span>, found)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> found == <span class="literal">nil</span> &#123;</span><br><span class="line">        fmt.Printf(<span class="string">&quot;no match found\n&quot;</span>)</span><br><span class="line">        os.Exit(<span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, word := <span class="keyword">range</span> found &#123;</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%s\n&quot;</span>, word)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们找到了单词 fox 的所有出现，包括它的复数形式。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">re := regexp.<span class="built_in">MustCompile</span>(<span class="string">&quot;(?i)fox(es)?&quot;</span>)</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>使用 (?i) 语法，正则表达式不区分大小写。 （es）？表示“es”字符可能包含零次或一次。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">found := re.<span class="built_in">FindAllString</span>(content, -<span class="number">1</span>)</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>我们使用 <code>FindAllString</code> 查找所有出现的已定义正则表达式。第二个参数是要查找的最大匹配项； -1 表示搜索所有可能的匹配项。</p>
<p>运行结果：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-attr">[<span class="string">&quot;Foxes&quot;</span> <span class="string">&quot;Foxes&quot;</span> <span class="string">&quot;Foxes&quot;</span> <span class="string">&quot;fox&quot;</span> <span class="string">&quot;fox&quot;</span>]</span></span><br><span class="line">Foxes</span><br><span class="line">Foxes</span><br><span class="line">Foxes</span><br><span class="line">fox</span><br><span class="line">fox</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="FindAllStringIndex-函数"><a href="#FindAllStringIndex-函数" class="headerlink" title="FindAllStringIndex 函数"></a>FindAllStringIndex 函数</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> content = <span class="string">`Foxes are omnivorous mammals belonging to several genera </span></span><br><span class="line"><span class="string">of the family Canidae. Foxes have a flattened skull, upright triangular ears, </span></span><br><span class="line"><span class="string">a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every </span></span><br><span class="line"><span class="string">continent except Antarctica. By far the most common and widespread species of </span></span><br><span class="line"><span class="string">fox is the red fox.`</span></span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">&quot;(?i)fox(es)?&quot;</span>)</span><br><span class="line"></span><br><span class="line">    idx := re.FindAllStringIndex(content, <span class="number">-1</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, j := <span class="keyword">range</span> idx &#123;</span><br><span class="line">        match := content[j[<span class="number">0</span>]:j[<span class="number">1</span>]]</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%s at %d:%d\n&quot;</span>, match, j[<span class="number">0</span>], j[<span class="number">1</span>])</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们在文本中找到所有出现的 fox 单词及其索引。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Foxes at 0:5</span><br><span class="line">Foxes at 81:86</span><br><span class="line">Foxes at 196:201</span><br><span class="line">fox at 296:299</span><br><span class="line">fox at 311:314</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="Split-函数"><a href="#Split-函数" class="headerlink" title="Split 函数"></a>Split 函数</h2><p><code>Split</code> 函数将字符串切割成由定义的正则表达式分隔的子字符串。它返回这些表达式匹配之间的子字符串切片。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;log&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">    <span class="string">&quot;strconv&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> data = <span class="string">`22, 1, 3, 4, 5, 17, 4, 3, 21, 4, 5, 1, 48, 9, 42`</span></span><br><span class="line"></span><br><span class="line">    sum := <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">&quot;,\s*&quot;</span>)</span><br><span class="line"></span><br><span class="line">    vals := re.Split(data, <span class="number">-1</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, val := <span class="keyword">range</span> vals &#123;</span><br><span class="line"></span><br><span class="line">        n, err := strconv.Atoi(val)</span><br><span class="line"></span><br><span class="line">        sum += n</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">            log.Fatal(err)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    fmt.Println(sum)</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们有一个逗号分隔的值列表。我们从字符串中截取值并计算它们的总和。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">re := regexp.MustCompile(<span class="string">&quot;,\s*&quot;</span>)</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>正则表达式包括一个逗号字符和任意数量的相邻空格。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vals := re.Split(data, <span class="number">-1</span>)</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>我们得到了值的一部分。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> _, val := <span class="keyword">range</span> vals &#123;</span><br><span class="line"></span><br><span class="line">    n, err := strconv.Atoi(val)</span><br><span class="line"></span><br><span class="line">    sum += n</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">        log.Fatal(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>我们遍历切片并计算总和。切片包含字符串；因此，我们使用 <code>strconv.Atoi</code> 函数将每个字符串转换为整数。</p>
<p>运行代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">189</span><br><span class="line">复制代码</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Go-正则表达式捕获组"><a href="#Go-正则表达式捕获组" class="headerlink" title="Go 正则表达式捕获组"></a>Go 正则表达式捕获组</h2><p>圆括号 () 用于创建捕获组。这允许我们将量词应用于整个组或将交替限制为正则表达式的一部分。 为了找到捕获组（Go 使用术语子表达式），我们使用 <code>FindStringSubmatch</code> 函数。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    websites := [...]<span class="type">string</span>&#123;<span class="string">&quot;webcode.me&quot;</span>, <span class="string">&quot;zetcode.com&quot;</span>, <span class="string">&quot;freebsd.org&quot;</span>, <span class="string">&quot;netbsd.org&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">&quot;(\w+)\.(\w+)&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, website := <span class="keyword">range</span> websites &#123;</span><br><span class="line"></span><br><span class="line">        parts := re.FindStringSubmatch(website)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> i, _ := <span class="keyword">range</span> parts &#123;</span><br><span class="line">            fmt.Println(parts[i])</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        fmt.Println(<span class="string">&quot;---------------------&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们使用组将域名分为两部分。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">re := regexp.MustCompile(<span class="string">&quot;(\w+)\.(\w+)&quot;</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>我们用括号定义了两个组。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">parts := re.FindStringSubmatch(website)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p><code>FindStringSubmatch</code> 返回包含匹配项的字符串切片，包括来自捕获组的字符串。</p>
<p>运行代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">$ go run capturegroups.go </span><br><span class="line">webcode.me</span><br><span class="line">webcode</span><br><span class="line">me</span><br><span class="line">---------------------</span><br><span class="line">zetcode.com</span><br><span class="line">zetcode</span><br><span class="line">com</span><br><span class="line">---------------------</span><br><span class="line">freebsd.org</span><br><span class="line">freebsd</span><br><span class="line">org</span><br><span class="line">---------------------</span><br><span class="line">netbsd.org</span><br><span class="line">netbsd</span><br><span class="line">org</span><br><span class="line">---------------------</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="正则表达式替换字符串"><a href="#正则表达式替换字符串" class="headerlink" title="正则表达式替换字符串"></a>正则表达式替换字符串</h2><p>可以用 <code>ReplaceAllString</code> 替换字符串。该方法返回修改后的字符串。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;io/ioutil&quot;</span></span><br><span class="line">    <span class="string">&quot;log&quot;</span></span><br><span class="line">    <span class="string">&quot;net/http&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">    <span class="string">&quot;strings&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    resp, err := http.Get(<span class="string">&quot;http://webcode.me&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">        log.Fatal(err)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">defer</span> resp.Body.Close()</span><br><span class="line"></span><br><span class="line">    body, err := ioutil.ReadAll(resp.Body)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line"></span><br><span class="line">        log.Fatal(err)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    content := <span class="type">string</span>(body)</span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">&quot;&lt;[^&gt;]*&gt;&quot;</span>)</span><br><span class="line">    replaced := re.ReplaceAllString(content, <span class="string">&quot;&quot;</span>)</span><br><span class="line"></span><br><span class="line">    fmt.Println(strings.TrimSpace(replaced))</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>该示例读取网页的 HTML 数据并使用正则表达式去除其 HTML 标记。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">resp, err := http.<span class="built_in">Get</span>(<span class="string">&quot;http://webcode.me&quot;</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>我们使用 http 包中的 Get 函数创建一个 GET 请求。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">body</span>, err := ioutil.<span class="built_in">ReadAll</span>(resp.Body)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>我们读取响应对象的主体。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">re := regexp.<span class="built_in">MustCompile</span>(<span class="string">&quot;&lt;[^&gt;]*&gt;&quot;</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>这个模式定义了一个匹配 HTML 标签的正则表达式。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">replaced := re.<span class="built_in">ReplaceAllString</span>(content, <span class="string">&quot;&quot;</span>)</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>我们使用 ReplaceAllString 方法删除所有标签。</p>
<h2 id="ReplaceAllStringFunc-函数"><a href="#ReplaceAllStringFunc-函数" class="headerlink" title="ReplaceAllStringFunc 函数"></a>ReplaceAllStringFunc 函数</h2><p><code>ReplaceAllStringFunc</code> 返回一个字符串的副本，其中正则表达式的所有匹配项都已替换为指定函数的返回值。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;regexp&quot;</span></span><br><span class="line">    <span class="string">&quot;strings&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    content := <span class="string">&quot;an old eagle&quot;</span></span><br><span class="line"></span><br><span class="line">    re := regexp.MustCompile(<span class="string">`[^aeiou]`</span>)</span><br><span class="line"></span><br><span class="line">    fmt.Println(re.ReplaceAllStringFunc(content, strings.ToUpper))</span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>在代码示例中，我们将 <code>strings.ToUpper</code> 函数应用于字符串的所有字符。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="keyword">go</span> run replaceallfunc.<span class="keyword">go</span> </span><br><span class="line">aN oLD eaGLe</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>模式匹配在根据基于正则表达式和语法的特定搜索模式在字符串中搜索某些字符集时起着重要作用。匹配的模式允许我们从字符串中提取所需的数据并以我们喜欢的方式对其进行操作。理解和使用正则表达式是处理文本的关键。</p>
<p>在实际过程中，程序员会保留一组常用的正则表达式来匹配电子邮件、电话号码等，并在需要时使用和重用它。</p>
<p>本文只是简单挑选了一些常见的函数进行介绍，其实 <code>regexp</code> 包还有更多的函数，留给读者自己去探索和研究。</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/05/09/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8FGolangPythonJavascript/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/javascript/" rel="tag">javascript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="tag">正则表达式</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/05/10/%E6%B5%8B%E8%AF%95-%E5%BC%80%E5%90%AFjmeter%E6%B5%8B%E8%AF%95/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            测试-开启jmeter测试
          
        </div>
      </a>
    
    
      <a href="/2023/05/09/%E5%85%A5%E9%97%A8-PythonJsBootStrapCSS%E4%B9%8B%E8%B7%AF/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">人生苦短,我用Python</div>
      </a>
    
  </nav>

  
   
  
    
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>